const express = require('express');
const router = express.Router();
const fs = require('fs');
const path = require('path');
const { DATA_DIR } = require('../constants/paths');

// 获取所有日期文件夹
router.get('/dates', (req, res) => {
  fs.readdir(DATA_DIR, (err, files) => {
      if (err) return res.status(500).json({ error: '无法读取目录' });
      
      const dateFolders = files.filter(file => /^\d{4}-\d{2}-\d{2}$/.test(file));
      dateFolders.sort((a, b) => b.localeCompare(a));
      
      res.json(dateFolders);
  });
});

// 获取某个日期的 summary 数据
router.get('/data/:date', (req, res) => {
  const date = req.params.date;
  const summaryFile = path.join(DATA_DIR, date, 'oneDaySummary.json');
  
  fs.readFile(summaryFile, 'utf8', (err, data) => {
      if (err) return res.status(404).json({ error: '数据未找到' });
      res.json(JSON.parse(data));
  });
});

module.exports = router;